package com.jwl.idc.http;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.jwl.idc.R;
import com.jwl.idc.actionbean.BaseBean;
import com.jwl.idc.paser.DataPaser;

import java.util.Map;

/**
 * Created by liangfan on 2015/12/31.
 * descript 关于http action请求一个实现类
 */
public class HttpActionExcute implements Runnable {


    //http key
    public static final int JWL_HTTP_LOGIN_KEY = 0x001;
    public static final int JWL_HTTP_REGISTER_KEY = 0x002;
    public static final int JWL_HTTP_SENDSMS_KEY = 0x003;
    public static final int JWL_HTTP_DEVICE_LIST_KEY = 0x004;

    //持有一个httpActionInterface
    private HttpActionBase actionAcallBack;
    private int key;
    private Map<String, Object> map;
    private Handler httpHandler;

    public HttpActionExcute() {
    }

    public HttpActionExcute(int key, Map<String, Object> map, HttpActionBase actionAcallBack) {
        this.key = key;
        this.actionAcallBack = actionAcallBack;
        this.map = map;
        initHandler();
    }

    void initHandler() {
        httpHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                //对错误的消息进行一些处理
                actionAcallBack.excuteFinish();
                switch (msg.what){
                    case 301:
                        Toast.makeText(actionAcallBack.mContext, actionAcallBack.mContext.getResources().getString(R.string.sanbainyi),Toast.LENGTH_LONG).show();
                        //返回到登录界面
                        break;
                    case 200:
                        actionAcallBack.onSuccess((BaseBean)msg.obj);
                        break;
                    case 310:
                        Toast.makeText(actionAcallBack.mContext, actionAcallBack.mContext.getResources().getString(R.string.sanbaiyi),Toast.LENGTH_SHORT).show();
                        break;
                    case 311:
                        Toast.makeText(actionAcallBack.mContext, actionAcallBack.mContext.getResources().getString(R.string.sanbaiyishiyi),Toast.LENGTH_SHORT).show();
                        break;
                    case 330:
                        Toast.makeText(actionAcallBack.mContext, actionAcallBack.mContext.getResources().getString(R.string.sanbaisan),Toast.LENGTH_SHORT).show();
                        break;
                    case 331:
                        Toast.makeText(actionAcallBack.mContext, actionAcallBack.mContext.getResources().getString(R.string.sanbaisanshiyi),Toast.LENGTH_SHORT).show();
                        break;
                    case 350:
                        Toast.makeText(actionAcallBack.mContext, actionAcallBack.mContext.getResources().getString(R.string.sanbaiwu),Toast.LENGTH_SHORT).show();
                        break;
                    case 351:
                        Toast.makeText(actionAcallBack.mContext, actionAcallBack.mContext.getResources().getString(R.string.sanbaiwushiyi),Toast.LENGTH_SHORT).show();
                        break;
                    case 352:
                        Toast.makeText(actionAcallBack.mContext, actionAcallBack.mContext.getResources().getString(R.string.sanbaiwushier),Toast.LENGTH_SHORT).show();
                        break;
                    case 353:
                        Toast.makeText(actionAcallBack.mContext, actionAcallBack.mContext.getResources().getString(R.string.sanbaiwushisan),Toast.LENGTH_SHORT).show();
                        break;
                    case 355:
                        Toast.makeText(actionAcallBack.mContext, actionAcallBack.mContext.getResources().getString(R.string.sanbaiwushiwu),Toast.LENGTH_SHORT).show();
                        break;
                    case 371:
                        Toast.makeText(actionAcallBack.mContext, actionAcallBack.mContext.getResources().getString(R.string.sanbaiqishiyi),Toast.LENGTH_SHORT).show();
                        break;
                    case 380:
                        Toast.makeText(actionAcallBack.mContext, actionAcallBack.mContext.getResources().getString(R.string.sanbaiba),Toast.LENGTH_SHORT).show();
                        break;
                    case 381:
                        Toast.makeText(actionAcallBack.mContext, actionAcallBack.mContext.getResources().getString(R.string.sanbaibashiyi),Toast.LENGTH_SHORT).show();
                        break;
                    case 385:
                        Toast.makeText(actionAcallBack.mContext, actionAcallBack.mContext.getResources().getString(R.string.sanbaibashiwu),Toast.LENGTH_SHORT).show();
                        break;
                    case 390:
                        Toast.makeText(actionAcallBack.mContext, actionAcallBack.mContext.getResources().getString(R.string.sanbaijiu),Toast.LENGTH_SHORT).show();
                        break;
                    case 400:
                        Toast.makeText(actionAcallBack.mContext, actionAcallBack.mContext.getResources().getString(R.string.sibai),Toast.LENGTH_SHORT).show();
                        break;
                    case 410:
                        Toast.makeText(actionAcallBack.mContext, actionAcallBack.mContext.getResources().getString(R.string.sibaiyi),Toast.LENGTH_SHORT).show();
                        break;
                }
                close();
            }
        };
    }


    void close(){
        actionAcallBack.mContext = null;
        actionAcallBack = null;
        httpHandler = null;
        map = null;
    }

    @Override
    public void run() {
        //根据结果来处理执行流程
        Message msg = httpHandler.obtainMessage();
        msg.arg1 = key;
        String data = "";
        BaseBean bean = null;
        try {
            switch (key) {
                case JWL_HTTP_LOGIN_KEY:
                    data = HttpClientApi._post(URLs.HTTP_LOGIN_URL, map, null);
                    bean = DataPaser.dataPaser(data, DataPaser.LOGIN_USER);
                    break;
                case JWL_HTTP_REGISTER_KEY:
                    data = HttpClientApi._post(URLs.HTTP_REGISTER_URL, map, null);
                    bean = DataPaser.dataPaser(data, DataPaser.REGISTER_USER);
                    break;
                case JWL_HTTP_DEVICE_LIST_KEY:
                    data = HttpClientApi._post(URLs.HTTP_DEVICE_LIST_URL, map, null);
                    bean = DataPaser.dataPaser(data, DataPaser.DEVICE_LIST_PASER);
                    break;
                case JWL_HTTP_SENDSMS_KEY:
                    data = HttpClientApi._post(URLs.HTTP_GET_SMS_URL,map,null);
                    bean = DataPaser.dataPaser(data, DataPaser.GET_SMS_PASER);
                    break;
            }
            Log.i("====","liangfan....data = "+data);
            msg.obj = bean;
            msg.what = bean.getResponse().getCode();
            httpHandler.sendMessage(msg);
        } catch (AppException e) {
            e.printStackTrace();
            msg.what = e.getCode();
            msg.obj = e;
            httpHandler.sendMessage(msg);
        }
    }
}
